<?php
/**
 * Created by PhpStorm.
 * User: admin
 * Date: 2018/6/7
 * Time: 1:51
 */
namespace App\Http\Controllers\Api\v1\User;

use App\Models\Api\User;
use App\Models\Api\Recode;
use App\Models\Api\MessageCenter;
use App\Models\Api\LanmeiUserCounselorStatus;
use Illuminate\Http\Request;
use App\Exceptions\ApiException;

use App\Http\Controllers\Api\BaseController;
use DB;

/**
 * @title
 * @desc
 * @url
 * @version 2.0.0
 * Class Demo
 * @package app\api\controller\v2
 */
class PersonalController extends BaseController
{
    protected $restActionList = ['index','read','save','update','delete'];

    /**
     * @title
     * @method GET
     * @desc  获取我的信息
     * @return array
     */
    public function index()
    {
        if(!Request()->isMethod('get'))
        {
            return show(config('code.error'), '未知操作', [], 403 );
        }
        //        将用户头像和档案头像统一
        $map =['uid'=>$request->uid,'isself'=>1];
        $rea = Recode::where($map)->first(['avatar']);
        if($rea){
          $rea =  $rea->toArray();
        }
        $MsgModel = new MessageCenter();
        $ra =$rea['avatar'];
        $ua = User::where('id',$request->uid)->first(['avatar','has_self_recode'])->toArray();
        $u =$ua['avatar'];
        if ($ra){
            if(!$ua|| $u==""){
               User::where(['id'=>$request->uid])->update(['avatar'=>$rea['avatar']]);
            }
        }
        if ($u){
            if(!$ra || $ra==""){
                Recode::where($map)->update(['avatar'=>$ua['avatar']]);
            }
        }
        if ($ra && $u){
            if (strcmp($rea['avatar'],$ua['avatar']) !== 0){
               Recode::where($map)->update(['avatar'=>$ua['avatar']]);
            }
        }
        $where =['r.uid'=>$request->uid,'r.isself'=>1];

        $recode = Recode::from('recode as r')
            ->join('recode_astro as ra','r.id','=','ra.rid','INNER')
            ->join('user as u','r.uid','=','u.id','LEFT')
            ->where($where)
            ->first(['r.id','r.name','r.uid','r.isself','ra.sun','ra.moon','ra.asc','u.sex','u.avatar','u.uen']);
        $msg_id =DB::table('user_msg')->where(['uid'=>$request->uid])->get(['mid'])->map(function ($value) {
            return (array)$value;
        })->toArray();
        if (!$recode['sex'])
        {
            $sex=1;
        }else{
            $sex=$recode['sex'];
        }
        if($ua['has_self_recode'] =='1'){
        $data = [
            'id'=>$recode['id'],
            'uen'=>$recode['uen'],
            'name'=>$recode['name'],
            'sex'=>$sex,
            'avatar'=>$recode['avatar'],
            'sun'=>$recode['sun'],
            'moon'=>$recode['moon'],
            'asc'=>$recode['asc'],
        ];
        }else{
           $data = [
            'id'=>$request->uid,
            'uen'=>$this->user['uen'],
            'name'=>$this->user['nickname'],
            'sex'=>$this->user['sex'],
            'avatar'=>$this->user['avatar'],
            'sun'=>'null',
            'moon'=>'null',
            'asc'=>'null',
        ];
        }


        //获取用户全部未读系统消息id
        $system_msg_id = $MsgModel->getAllUserMessage(['uid'=>$request->uid,'type'=>1]);
        //获取用户全部未读服务消息id
        $server_msg_id = $MsgModel->getAllUserMessage(['uid'=>$request->uid,'type'=>2]);
        $msg_count = count($system_msg_id) +count($server_msg_id);
        //        获取所有回复数
       /* $replys = DB::table('wechatadmin.system_comment_reply')->alias('cr')
            ->field('cr.id,cr.create_time,cr.content,cr.aid,cr.pid,a.title,
            u.nickname,u.avatar,
            u1.nickname nickname1,u1.avatar avatar1,ac.content content1,
            u2.nickname nickname2,u2.avatar avatar2,cr2.content content2')
            ->join('wechatadmin.system_article_comment ac','cr.comment_id=ac.id','LEFT')
            ->join('wechat.app_user u1','ac.ucid=u1.uc_id','LEFT')
            ->join('wechatadmin.system_comment_reply cr2','cr.pid=cr2.id','LEFT')
            ->join('wechat.app_user u2','cr2.ucid=u2.uc_id','LEFT')
            ->join('wechatadmin.system_article a','cr.aid=a.id','LEFT')
            ->join('wechatadmin.system_article_comment ac2','cr.comment_id=ac2.id','LEFT')
            ->join('wechat.app_user u','cr.ucid=u.uc_id','LEFT')
            ->where("ac2.status=0
            AND ((cr.pid=0 AND ac.ucid='{$this->user['uc_id']}') OR (cr.pid>0 AND cr2.ucid='{$this->user['uc_id']}'))")
            ->order('cr.id DESC')
//            ->fetchSql(true)
//            ->select();
            ->select();
        $replys =json_decode(json_encode($replys,true),true);*/
        //      获取用户已读的回复id
        /*$reply_id =DB::table('user_reply')->where(['uid'=>$request->uid])->get(['reply_id']);
        $reply=[];
        foreach ($replys as $k=>$v)
        {
            foreach ($reply_id as $kk=>$vv)
            {
                if ($v['id']==intval($vv['reply_id']))
                {
                    $reply[] =$v;
                }
            }
        }*/
        //$reply_count =['reply_count'=>count($replys) - count($reply)];
        //$reply_count =['reply_count'=>'2'];
        $array1['msg_count'] =$msg_count;

        // 获取咨询师身份信息
        $userCounselorStatus = (new LanmeiUserCounselorStatus())->getPersonalCounselorInfo($request->uid);
        $array=array_merge($data,$array1,$userCounselorStatus);
        return show(200,'成功',$array);
    }
}
